Page({
    data: {
        name: '', // 试卷名称
        examList: null, //题目list
        total: 0, // 题目总数
        answerList: null,
        currentPage : 0, //当前Page
        first : true, // Page是否为first
        last : false, // page是否为false
        current: '', // 记录点击内容
        userId: '',
    },
    // 点击选项的响应函数
    handleChoiceChange({ detail = {} }) {
        // 获取选项
        var chosen = detail.value.split('、')[0]
        // 获取内容
        var content = detail.value.split('、')[1]
        // 获取当前题目编号
        var i = this.data.currentPage
        this.setData({
            [`answerList[${i}].display`]: detail.value,
            // 将答案列表对应项填入
            [`answerList[${i}].option`] : chosen,
            [`answerList[${i}].userAnswer`] : content
        });
    },
    // 点击上一题的响应函数
    lastPage(){
        // 判断是否到第一页，是否隐藏上一页按钮
        var current_page =  this.data.currentPage
        if (current_page == 1) {
            this.setData({
                currentPage: current_page - 1,
                first: true,
                last: false,
                current: this.data.answerList[current_page].display
            });
        } else {
            this.setData({
                currentPage: current_page - 1,
                first: false,
                last: false,
                current: this.data.answerList[current_page].display
            });
        }
    },
    // 点击下一题的响应函数
    nextPage(){
        // 判断是否到最后页，是否隐藏下一页按钮
        var current_page =  this.data.currentPage
        if (current_page == this.data.total-2) {
            this.setData({
                currentPage: current_page + 1,
                first: false,
                last: true,
                current: this.data.answerList[current_page].display
            });
        } else {
            this.setData({
                currentPage: current_page + 1,
                first: false,
                last: false,
                current: this.data.answerList[current_page].display
            });
        }
    },

    onLoad: function (options) {
        var app = getApp();
        var url = app.globalData.global_url;
        // 获取从试卷列表页传来的name参数，name为试卷名称
        this.setData({
            name: options.name
        });
        var that = this;
        wx.login({
            success (res) {
                if (res.code) {
                    //发起网络请求,查询并保存用户id
                    wx.request({
                        url: url+'/user/getOpenId',//'http://localhost:8000/user/getOpenId',
                        data: {
                          code: res.code
                        },
                        success: function (response) {
                            if(response.statusCode == 200){
                                that.setData({
                                    userId : response.data
                                })
                            }
                            else{
                                wx.showToast({
                                    title: '获取用户标识符失败',
                                    icon: 'error',
                                    duration: 2000
                                })
                            }
                        },
                        fail:function(res){
                            wx.showToast({
                                title: '服务器错误',
                                icon: 'error',
                                duration: 2000
                            })
                        }
                    })
                }
                else {
                    wx.showToast({
                        title: '获取用户标识符失败',
                        icon: 'error',
                        duration: 2000
                    })
                    console.log('登录失败！' + res.errMsg)
                }
            }
        })
        // 获取试卷内容
        wx.request({
            url: url+'/exam?type=exam&name='+this.data.name,//'http://localhost:8000/exam?type=exam&name='+this.data.name,
            method: 'GET',
            header: {
            'content-type': 'application/json' // 默认值
            },
            success:res=>{
                // 初始化数据
                if(res.statusCode == 200){
                    that.setData({
                        examList: res.data.examList,
                        answerList: res.data.answerSheet,
                        total: res.data.size,
                        loading: false
                    })
                }
                else{
                    wx.showToast({
                        title: '获取试卷内容失败',
                        icon: 'error',
                        duration: 2000
                    })
                }
            },
            fail:function(res){
                wx.showToast({
                    title: '服务器错误',
                    icon: 'error',
                    duration: 2000
                })
            }
        })
    },

    // 点击答题卡响应函数，跳转到答题卡页面
    answerSheet(){
        console.log(this.data.userId)
        wx.navigateTo({
          url: '../answerSheetPage/index?examName='+this.data.name+'&userId='+this.data.userId+'&total='+this.data.total+
          '&answerSheet='+JSON.stringify(this.data.answerList),
        })
    },

});